//
//  PhotoMainViewController.m
//  OfficeEtc
//
//  Created by Wangli on 12-6-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "PhotoMainViewController.h"
#import "AppDelegate.h"
#import "RootViewController.h"
#import "PhotoEntity.h"
#import <CoreLocation/CoreLocation.h>
#import "BusinessOperation.h"
@interface PhotoMainViewController ()

@end

@implementation PhotoMainViewController
@synthesize m_PhotoPath;
@synthesize m_ThumPhotoPath;
-(id) init
{
    if (self = [super init]) {
        NSArray *docFolders = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ); 
        NSString* tDocPath = [docFolders lastObject];
        m_PhotoPath = [tDocPath stringByAppendingPathComponent:@"photo"];
        [m_PhotoPath retain];
        
        NSArray *libFolders = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES ); 
        NSString* tlibPath = [libFolders lastObject];
        m_ThumPhotoPath = [tlibPath stringByAppendingPathComponent:@"Thumbnail"];
        [m_ThumPhotoPath retain];
        
        p_OperationQueue = [[NSOperationQueue alloc] init];
        [p_OperationQueue setMaxConcurrentOperationCount:1];
    }
    return self;
}

-(void) dealloc
{
    [m_ThumPhotoPath release];
    [p_OperationQueue release];
    [m_PhotoPath release];
    [p_EditBarBt release];
    [p_FullImageArr release];
    [p_ThumbnailArr release];
    [p_BottomBar release];
    [p_ListTableView release];
    [super dealloc];
}



#pragma mark -
#pragma mark UIViewControl Methods

- (void)viewDidLoad
{
    _sourceType = kSourceImage;
    [super viewDidLoad];
    
    
	// Do any additional setup after loading the view.
    p_ThumbnailArr = [[NSMutableArray alloc] init];
    p_FullImageArr = [[NSMutableArray alloc] init];
    [self getAllSqlEntity];
    p_ListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height-44) style:UITableViewStylePlain];
    p_ListTableView.separatorColor =[UIColor clearColor];
    p_ListTableView.delegate = self;
    p_ListTableView.dataSource = self;
    [self.view addSubview:p_ListTableView];
    
    
    p_BottomBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 480-20-40-44, 320, 40)];
    p_BottomBar.tintColor = [UIColor lightGrayColor];
    [self.view addSubview:p_BottomBar];
    
    UIBarButtonItem* tBarBtItem0 = [[UIBarButtonItem alloc] initWithTitle:@"图片库" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem0.width =55;
    tBarBtItem0.tag = 30;
    UIBarButtonItem* tBarBtItem1 = [[UIBarButtonItem alloc] initWithTitle:@"wifi" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem1.width =55;
    tBarBtItem1.tag = 31;
    UIBarButtonItem* tBarBtItem2 = [[UIBarButtonItem alloc] initWithTitle:@"ftp" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem2.width =55;
    tBarBtItem2.tag = 32;
    UIBarButtonItem* tBarBtItem3 = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem3.width =75;
    tBarBtItem3.tag = 33;
    UIBarButtonItem* tBarBtItem4 = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem4.width =55;
    tBarBtItem4.tag = 33;
    [p_BottomBar setItems:[NSArray arrayWithObjects:tBarBtItem0,tBarBtItem1,tBarBtItem2,tBarBtItem3,tBarBtItem4,nil]];
    [tBarBtItem0 release];
    [tBarBtItem1 release];
    [tBarBtItem2 release];
    [tBarBtItem3 release];
    [tBarBtItem4 release];
    p_BottomBar.hidden =YES;

    //没有照片的时候，右上角为“添加”;
    //有照片的时候，右上角为“编辑”
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                                                            target:self
                                                                                            action:@selector(addPhotoAction)] autorelease];
    
    p_EditBarBt = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(docEditMode:)];
    if([p_ThumbnailArr count] > 0)
    {
        self.navigationItem.rightBarButtonItem= p_EditBarBt;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}


#pragma mark-
#pragma mark 私有内部函数
-(void) docEditMode:(UIBarButtonItem*)f_sender
{
    if ([f_sender.title isEqualToString:@"编辑"]) {
        f_sender.title =@"完成";
        if (p_BottomBar.hidden) {
            [self showBottomTool];
        }
    }else {
        f_sender.title =@"编辑";
        if (!p_BottomBar.hidden) {
            [self hideBottomTool];
        }
    }
}

-(void) toolButtonPressed:(UIBarButtonItem*) f_sender
{
    switch (f_sender.tag) {
        case 30://图片库
        {
            [self performSelector:@selector(addPhotoAction)];
        }
            break;
        case 31://wifi
        {
            WifiViewController* tWifiVC = [[WifiViewController alloc] init];
            if ([tWifiVC networkAvailable]) {
                tWifiVC.m_OperationPath =m_PhotoPath;
                tWifiVC.m_Delegate = self;
                UINavigationController* tRootNav = [[UINavigationController alloc] initWithRootViewController:tWifiVC];
                [tWifiVC release];
                [self presentModalViewController:tRootNav animated:YES];
                [tRootNav release];
            }else {
                [tWifiVC release];
                UIAlertView* tAlertWifi = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"没有检测到wifi信号请稍后再试！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
                [tAlertWifi show];
                [tAlertWifi release];
            }
        }
            break;
        case 32://ftp
        {
            AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
            UINavigationController* tRootNav = (UINavigationController*)tAppDel.window.rootViewController;
            RootViewController* tRootVC = (RootViewController*)[tRootNav.viewControllers objectAtIndex:0];
            [tRootVC FTPServerShow:m_PhotoPath];
        }
            break;
        case 33://删除
        {
            
        }
            break;
        case 34://删除
        {
            
        }
            break;
    }
}

-(void) showBottomTool
{
    p_BottomBar.alpha =0;
    p_BottomBar.hidden =NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.3f];
    p_BottomBar.alpha =1.0f;
    p_ListTableView.frame =CGRectMake(0, 0, 320, 480-104);
    [UIView commitAnimations];
}
-(void) hideBottomTool
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationStop)];
    p_BottomBar.alpha =0.f;
    p_ListTableView.frame =CGRectMake(0, 0, 320, 480-64);
    [UIView commitAnimations];
}
-(void)animationStop
{
    p_BottomBar.hidden =YES;
}
#pragma mark-
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 78.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
#pragma mark-
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [p_ThumbnailArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = [indexPath row];
    static NSString* tNameDeCell = @"photomorecell";
    UITableViewCell* tCell = nil;
    tCell = [tableView dequeueReusableCellWithIdentifier:tNameDeCell];
    if (tCell == nil) {
        tCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tNameDeCell] autorelease];
        tCell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIButton* tBtt0 = [UIButton buttonWithType:UIButtonTypeCustom];
        tBtt0.frame =CGRectMake(5, 0, 73, 75);
        tBtt0.tag =-1;
        [tCell addSubview:tBtt0];
        
        UIButton* tBtt1 = [UIButton buttonWithType:UIButtonTypeCustom];
        tBtt1.frame =CGRectMake(84, 0, 73, 75);
        tBtt1.tag =-2;
        [tCell addSubview:tBtt1];
        
        UIButton* tBtt2 = [UIButton buttonWithType:UIButtonTypeCustom];
        tBtt2.frame =CGRectMake(163, 0, 73, 75);
        tBtt2.tag =-3;
        [tCell addSubview:tBtt2];
        
        UIButton* tBtt3 = [UIButton buttonWithType:UIButtonTypeCustom];
        tBtt3.frame =CGRectMake(242, 0, 73, 75);
        tBtt3.tag =-4;
        [tCell addSubview:tBtt3];
    }
    int index0,index1,index2,index3;
    index0 = row*4;
    index1 = row*4+1;
    index2 = row*4+2;
    index3 = row*4+3;
    
   
    UIButton* tBtt0 = (UIButton*)[tCell viewWithTag:-1];
    UIButton* tBtt1 = (UIButton*)[tCell viewWithTag:-2];
    UIButton* tBtt2 = (UIButton*)[tCell viewWithTag:-3];
    UIButton* tBtt3 = (UIButton*)[tCell viewWithTag:-4];
    
    [tBtt0 setImage:nil forState:UIControlStateNormal];
    [tBtt1 setImage:nil forState:UIControlStateNormal];
    [tBtt2 setImage:nil forState:UIControlStateNormal];
    [tBtt3 setImage:nil forState:UIControlStateNormal];
    
    if (index0 < [p_ThumbnailArr count]) {
        NSDictionary* tThImgDic =[p_ThumbnailArr objectAtIndex:index0];
        [tBtt0 setImage:[tThImgDic objectForKey:@"thum"] forState:UIControlStateNormal];
    }
    if (index1 < [p_ThumbnailArr count]) {
        NSDictionary* tThImgDic =[p_ThumbnailArr objectAtIndex:index1];
        [tBtt1 setImage:[tThImgDic objectForKey:@"thum"] forState:UIControlStateNormal];
    }
    if (index2 < [p_ThumbnailArr count]) {
        NSDictionary* tThImgDic =[p_ThumbnailArr objectAtIndex:index2];
        [tBtt2 setImage:[tThImgDic objectForKey:@"thum"] forState:UIControlStateNormal];
    }
    if (index3 < [p_ThumbnailArr count]) {
        NSDictionary* tThImgDic =[p_ThumbnailArr objectAtIndex:index3];
        [tBtt3 setImage:[tThImgDic objectForKey:@"thum"] forState:UIControlStateNormal];
    }
    return tCell;
}


-(void) newImgaeAddToFlash:(NSDictionary*)f_FinishDic
{
    NSString* tThumPath = [f_FinishDic objectForKey:@"thum"];
    NSString* tSrcPath = [f_FinishDic objectForKey:@"src"];
    if ([tThumPath isEqualToString:@"fail"]||[tSrcPath isEqualToString:@"fail"]) {
        [p_ListTableView reloadData];
        self.navigationItem.rightBarButtonItem= p_EditBarBt; 
        return;
    }
    AppDelegate* tAppDelegate =(AppDelegate*)[UIApplication sharedApplication].delegate;
    NSError* tError = nil;
    NSFetchRequest* tIsExist =[[NSFetchRequest alloc] init];
    NSEntityDescription* tFindEntity = [NSEntityDescription entityForName:@"PhotoEntity" inManagedObjectContext:tAppDelegate.managedObjectContext];
    NSPredicate* tPredicate = [NSPredicate predicateWithFormat:@"(srcimgurl == %@)AND(thumbnaiurl == %@)",tSrcPath,tThumPath];
    [tIsExist setEntity:tFindEntity];
    [tIsExist setPredicate:tPredicate];
    NSArray* tFindArr = [tAppDelegate.managedObjectContext executeFetchRequest:tIsExist error:&tError];
    [tIsExist release];
    if ([tFindArr count] >0) {
        PhotoEntity* tChangedEntity =[tFindArr lastObject];
        tChangedEntity.tolocalstate= [NSNumber numberWithInt:ToLocalTrans_None];
        [tAppDelegate.managedObjectContext save:&tError];
        if (tError!= nil) {
            NSLog(@"转化本地数据 改变属性时错误 %@",[tError description]);
        }else {
            //通知界面刷新
            NSDictionary* tMapDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                     tSrcPath,@"src",
                                     [UIImage imageWithContentsOfFile:tThumPath],@"thum",
                                     nil];
            [p_ThumbnailArr addObject:tMapDic];
            [p_ListTableView reloadData];
            self.navigationItem.rightBarButtonItem= p_EditBarBt; 
        }
    }else {
        NSLog(@"转化到本地的数据 数据库中未找到 path =%@ thum =%@",tSrcPath,tThumPath);
    }
}


#pragma mark- 
#pragma mark Photo Methods
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info {
    
    int addIndex =0;
    for(NSDictionary *dict in info)
    {
        NSLog(@"%@",[dict description]);
        NSLog(@"%@",[dict allKeys]);
        ALAsset* tImgAsset = [dict objectForKey:@"SourceAsset"];
        
        NSString* tTimeName =[self nsdateToNSString:[NSDate date]];
        if (addIndex > 0) {
            tTimeName = [NSString stringWithFormat:@"%@%d",tTimeName,addIndex];
        }
        NSString *pngThumFilePath = [m_ThumPhotoPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",tTimeName]];
       
        NSString *pngSrcFilePath = [m_PhotoPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",tTimeName]];
        NSLog(@"%@",pngThumFilePath);
        NSLog(@"%@",pngSrcFilePath);
        
        AppDelegate* tAppDelegate =(AppDelegate*)[UIApplication sharedApplication].delegate;
        NSError* tError = nil;
        
        NSFetchRequest* tIsExist =[[NSFetchRequest alloc] init];
        NSEntityDescription* tFindEntity = [NSEntityDescription entityForName:@"PhotoEntity" inManagedObjectContext:tAppDelegate.managedObjectContext];
        NSPredicate* tPredicate = [NSPredicate predicateWithFormat:@"srcimgurl == %@",pngSrcFilePath];
        [tIsExist setEntity:tFindEntity];
        [tIsExist setPredicate:tPredicate];
        NSArray* tFindArr = [tAppDelegate.managedObjectContext executeFetchRequest:tIsExist error:&tError];
        [tIsExist release];
        PhotoEntity* tInEntDes = nil;
        if ([tFindArr count] > 0) {
            //图片已经存在了 覆盖操作 
            tInEntDes = [tFindArr lastObject];
            NSFileManager* tFManager =[NSFileManager defaultManager];
            [tFManager removeItemAtPath:tInEntDes.srcimgurl error:&tError];
            if (tError != nil) {
                NSLog(@"删除已存在的图片失败");
            }
            [tFManager removeItemAtPath:tInEntDes.thumbnaiurl error:&tError];
            if (tError != nil) {
                NSLog(@"删除已存在的缩略图片失败");
            }
        }else {
            tInEntDes = (PhotoEntity*)[NSEntityDescription insertNewObjectForEntityForName:@"PhotoEntity" inManagedObjectContext:tAppDelegate.managedObjectContext];
        }
        
        CLLocation* tLoc =[tImgAsset valueForProperty:ALAssetPropertyLocation];
        NSDate* tPhotoDate =[tImgAsset valueForProperty:ALAssetPropertyDate];
        NSDictionary* tPhotoURL =[tImgAsset valueForProperty:ALAssetPropertyURLs];
        NSURL* tURL = [tPhotoURL objectForKey:[[tPhotoURL allKeys] lastObject]];
        tInEntDes.thumbnaiurl = pngThumFilePath;
        tInEntDes.srcimgurl =pngSrcFilePath;
        tInEntDes.imgname = tTimeName;
        tInEntDes.imgpos =[NSString stringWithFormat:@"%f,%f",tLoc.coordinate.latitude,tLoc.coordinate.longitude];
        tInEntDes.imgdate = tPhotoDate;
        tInEntDes.tolocalstate = [NSNumber numberWithInt:ToLocalTrans_Opering];
        tInEntDes.asseturl = [tURL absoluteString];
        [tAppDelegate.managedObjectContext save:&tError];
        if (tError != nil) {
            NSLog(@"save new local music in database error =%@",[tError description]);
        }
        
        NSDictionary* tPassDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [tURL absoluteString],@"liburl", 
                                  pngThumFilePath,@"thumpath", 
                                  pngSrcFilePath,@"srcpath", 
                                  nil];
         
        BusinessOperation* tBOperation = [[BusinessOperation alloc] init];
        tBOperation.target =self;
        tBOperation.selector = @selector(writeImgToLocal:);
        tBOperation.object = tPassDic;
        [p_OperationQueue addOperation:tBOperation];
        [tBOperation release];
        
        addIndex++;
        
//         key:@"SourceAsset", 值类型：ALAsset*(该资源的asset)
//         key:@"image_ext", 字符串：资源后缀名，比如jpg,png等
        


//        [p_ThumbnailArr addObject:[UIImage imageWithCGImage:tImgAsset.thumbnail]];
//        [p_FullImageArr addObject:[tImgAsset defaultRepresentation]];
        
        //UIImageWriteToSavedPhotosAlbum
//         获得图像：
//         if([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
//         {
//            [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]]
//         }
//         else
//         {
//            [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage] scale:1.0 orientation:[[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue]]
//         }
//         
//         获得缩略图(75*75或150*150)，显示的cell用75差不多，如果缩略图生成成功，可以参考ELCAsset生成缩略图的tableview，节约时间
//         ALAsset:thumbnail
//         
//         ex:UIImage* thumbImage = [UIImage imageWithCGImage:[self.asset thumbnail]];
//          [workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
//         */
        

        
    }
	[self dismissModalViewControllerAnimated:YES];
}


-(void) getAllSqlEntity
{
    AppDelegate* tAppDelegate =(AppDelegate*)[UIApplication sharedApplication].delegate;
    NSError* tError = nil;
    
    NSFetchRequest* tIsExist =[[NSFetchRequest alloc] init];
    NSEntityDescription* tFindEntity = [NSEntityDescription entityForName:@"PhotoEntity" inManagedObjectContext:tAppDelegate.managedObjectContext];
    [tIsExist setEntity:tFindEntity];
    NSArray* tFindArr = [tAppDelegate.managedObjectContext executeFetchRequest:tIsExist error:&tError];
    [tIsExist release];
    
    if (p_FullImageArr) {
        [p_FullImageArr release];
        p_FullImageArr = nil;
    }
    p_FullImageArr = [NSMutableArray arrayWithArray:tFindArr]; 
    [p_FullImageArr retain];
    
    [p_ThumbnailArr removeAllObjects];
    for (PhotoEntity* tCheck in p_FullImageArr) {
        NSDictionary* tMapDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                 tCheck.srcimgurl,@"src",
                                 [UIImage imageWithContentsOfFile:tCheck.thumbnaiurl],@"thum",
                                 nil];
        [p_ThumbnailArr addObject:tMapDic];
    }
    [p_ListTableView reloadData];
}

#pragma mark-
#pragma mark 放缩图片
-(UIImage*) scaleImagefromImage:(UIImage*)srcimg :(CGSize)size
{
	UIImage* scalecov = nil;
	UIGraphicsBeginImageContext(size);
	[srcimg drawInRect:CGRectMake(0, 0, size.width, size.height)];
	scalecov = UIGraphicsGetImageFromCurrentImageContext();
	return scalecov;
}

-(void) writeImgToLocal:(NSDictionary*) f_ImgDic
{
    NSString* tAssetURL = [f_ImgDic objectForKey:@"liburl"];
    NSString* pngThumFilePath = [f_ImgDic objectForKey:@"thumpath"];
    NSString* pngSrcFilePath = [f_ImgDic objectForKey:@"srcpath"];
    
    ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
    // Try to load asset at mediaURL
    [library assetForURL:[NSURL URLWithString:tAssetURL] resultBlock:^(ALAsset *asset) {
        // If asset exists
        if (asset) {
            // Type your code here for successful
            UIImage* tThumImg= [UIImage imageWithCGImage:asset.thumbnail];
            UIImage* tSrcImg= [UIImage imageWithCGImage:[asset defaultRepresentation].fullScreenImage];
            
            if (tThumImg == nil) {
                if ([asset defaultRepresentation].fullScreenImage) {
                    tThumImg = [self scaleImagefromImage:tSrcImg :CGSizeMake(75, 75)];
                }else {
                    NSLog(@"图片源数据都为空! %@",[f_ImgDic description]);   
                }
            }
            
            NSData *tThumData = nil;
            NSFileManager* tFManager = [NSFileManager defaultManager];
            if (tThumImg) {
                tThumData = [NSData dataWithData:UIImagePNGRepresentation(tThumImg)];
                if (![tFManager fileExistsAtPath:pngThumFilePath]) {
                    [tThumData writeToFile:pngThumFilePath atomically:YES];
                }else {
                    NSLog(@"缩略图路径存在！！！");
                }
            }else {
                NSLog(@"%@,缩略图不存在",pngThumFilePath);
            }
            
            NSData *tSrcData = nil;
            if (tSrcImg) {
                tSrcData = [NSData dataWithData:UIImagePNGRepresentation(tSrcImg)];
                if (![tFManager fileExistsAtPath:pngSrcFilePath]) {
                    [tSrcData writeToFile:pngSrcFilePath atomically:YES];
                }else {
                    NSLog(@"实际图路径存在！！！");
                }
            }else {
                NSLog(@"%@,实际图不存在",pngThumFilePath);
            }
            NSDictionary* tFnishDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                       pngThumFilePath,@"thum",
                                       pngSrcFilePath,@"src",
                                       nil];
            [self performSelectorOnMainThread:@selector(newImgaeAddToFlash:) withObject:tFnishDic waitUntilDone:NO];
        } else {
            // Type your code here for not existing asset
            NSDictionary* tFnishDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                       @"fail",@"thum",
                                       @"fail",@"src",
                                       nil];
            [self performSelectorOnMainThread:@selector(newImgaeAddToFlash:) withObject:tFnishDic waitUntilDone:NO];
        }
    } failureBlock:^(NSError *error) {
        // Type your code here for failure (when user doesn't allow location in your app)
        NSDictionary* tFnishDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"fail",@"thum",
                                   @"fail",@"src",
                                   nil];
        [self performSelectorOnMainThread:@selector(newImgaeAddToFlash:) withObject:tFnishDic waitUntilDone:NO];
    }];

}

-(NSString*) nsdateToNSString:(NSDate*)f_Date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat : @"yyyy年M月d日H点m分s秒"];
    [formatter autorelease];
    return [formatter stringFromDate:f_Date];
}

#pragma mark -
#pragma mark UIImagePickerControllerDelegate



// this get called when an image has been chosen from the library or taken from the camera
//
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//    NSLog(@"%@",[info description]);
//    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
   
	[_imagePickerController dismissModalViewControllerAnimated:YES];
	
    
}


#pragma mark-
#pragma mark WifiAddFileProtocol
-(void) fileAddByWifiFinish:(NSString*) f_FilePath
{

}
-(void) fileDeleteByWifiFinish:(NSString*) f_FilePath
{

}

#pragma mark-
#pragma mark AudiFileChangedByFTP
-(void) addFileByFTP:(NSString*)f_Path
{

}
-(void) delFileByFTP:(NSString*)f_Path
{
    

}
-(void) reNameFileByFTP:(NSString*)f_Path oldName:(NSString*)f_OldName
{

}

@end
